package com.yami.shop.platform.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yami.shop.bean.enums.AuditStatus;
import com.yami.shop.bean.model.ShopCompany;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.service.ShopCompanyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @Author lth
 * @Date 2021/8/4 14:44
 */
@RestController
@AllArgsConstructor
@RequestMapping("/platform/shopCompany")
@Tag(name = "商家工商信息")
public class ShopCompanyController {

    private final ShopCompanyService shopCompanyService;

    @GetMapping
    @Operation(summary =  "根据店铺id获取店铺工商信息", description = "根据店铺id获取店铺工商信息")
    @Parameter(name = "shopId", description = "店铺id")
    public ServerResponseEntity<ShopCompany> getShopCompanyByShopId(@RequestParam("shopId") Long shopId) {
        ShopCompany shopCompany = shopCompanyService.getOne(Wrappers.lambdaQuery(ShopCompany.class).eq(ShopCompany::getShopId, shopId).eq(ShopCompany::getStatus, AuditStatus.SUCCESSAUDIT.value()));
        if (Objects.isNull(shopCompany)) {
            shopCompany = new ShopCompany();
        }
        return ServerResponseEntity.success(shopCompany);
    }

    @PutMapping
    @Operation(summary =  "更新店铺工商信息", description = "更新店铺工商信息")
    public ServerResponseEntity<Void> editShopCompany(@RequestBody @Valid ShopCompany shopCompany) {
        shopCompanyService.updateByShopId(shopCompany);
        return ServerResponseEntity.success();
    }

    @GetMapping("/checkCreditCode")
    @Operation(summary =  "检查统一信用码是否已存在", description = "检查统一信用码是否已存在")
    @Parameter(name = "creditCode", description = "信用码")
    public ServerResponseEntity<Boolean> checkCreditCode(@RequestParam(name="creditCode") String creditCode) {
        long count = shopCompanyService.count(Wrappers.lambdaQuery(ShopCompany.class)
                .eq(ShopCompany::getCreditCode, creditCode).eq(ShopCompany::getStatus, AuditStatus.SUCCESSAUDIT.value())
        );
        return ServerResponseEntity.success(count > 0);
    }
}
